package com.robot.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.robot.dao.TestCaseMapper;
import com.robot.model.TestCase;
import com.robot.vo.ResponseMessage;
import com.robot.vo.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/10/20
 * Time: 18:15
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Slf4j
@Service
public class TestCaseService {

    @Autowired
    TestCaseMapper testCaseMapper;
    /**
     * 新增测试用例
     * **/
    public ResponseMessage addTestCase(TestCase testCase){
        try{
            /**调用一个mapper的新增方法，参考接口那块**/
            int i =testCaseMapper.addTestCase(testCase);
            if(i!=1){
                return ResponseMessage.errorResponse("新增测试用例异常");
            }
        }catch (Exception e){
            log.error("新增测试用例异常",e);
            return ResponseMessage.errorResponse("新增测试用例异常");
        }

        return ResponseMessage.successResponse();
    }
    /**
     * 修改测试用例
     * **/
    public ResponseMessage modifyTestCase(TestCase testCase){
        try{
            /**调用一个mapper的新增方法，参考接口那块**/
            int i =testCaseMapper.updateTestCase(testCase);
            if(i!=1){
                return ResponseMessage.errorResponse("修改测试用例异常");
            }

        }catch (Exception e){
            log.error("修改测试用例异常",e);
            return ResponseMessage.errorResponse("修改测试用例异常");
        }
        return ResponseMessage.successResponse();
    }
    /**
     * 测试用例列表
     * **/
    public ResponseMessage testCaseList(TestCase testCase){
        Page page=PageHelper.startPage(testCase.getPageNum(),testCase.getPageSize(),true);
        List<TestCase> testCaseList= testCaseMapper.getTestCaseList(testCase);
        TablePage tablePage=new TablePage();
        tablePage.setRows(testCaseList);
        tablePage.setTotal(page.getTotal());
        return ResponseMessage.successResponse(tablePage);
    }
    public ResponseMessage delete(Integer[] caseIds){
        List ids = Arrays.asList(caseIds);
        try {
            testCaseMapper.removeTestCase(ids);
        }catch (Exception e){
            log.error("删除用例异常",e);
            return ResponseMessage.errorResponse("删除用例异常");
        }
        return ResponseMessage.successResponse();
    }
}
